/**
 * 内蒙古自治区采购网爬虫
 * 
 */
import {Base,Item} from '../base';
//const iconv = require('iconv-lite');
class nmgzzqcg extends Base{
    document:Document;
    constructor(){
        super();
        this.name = "内蒙古自治区采购网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        let num = pageIndex + 1;
        let url = `http://www.nmgp.gov.cn/zfcgwslave/web/index.php?r=zfcgw%2Fanndata&type_name=1&byf_page=${num}&fun=cggg&_=1516095201421`;
        let result:Item[] = [];
        let content = this.spider.getSync(url);
        let jData = JSON.parse(content);
        let that =this;
        jData[0].forEach(function(ele:any,index:any){
            let item = that.buildItem();
            (<any>item)['公告日期'] = ele['SUBDATE'];
            item.title = ele['TITLE_ALL'];
            item.source = that.name;
            let uri = 'http://www.nmgp.gov.cn/ay_post/post.php?tb_id=1&p_id=' + ele['wp_mark_id'];
            (<any>item).url = 'http://www.nmgp.gov.cn/ay_post/post.php?tb_id=1&p_id=' + ele['wp_mark_id'];
            result = that.getExtraItem(uri, item, result);
        });
        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content,{decodeEntities: false});
        (<any>item).content =$('#center').html();
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new nmgzzqcg();